package cn.summit;

import java.util.HashMap;
import java.util.Map;
import java.util.Stack;

/**
 * @author summit
 * @since 2021/8/15 18:23
 */
public class 有效括号 {

	public static Map<Character, Character> map = new HashMap<>(4);

	static {
		map.put('(', ')');
		map.put('[', ']');
		map.put('{', '}');
	}

	private static Boolean doVerify(String str) {
		char[] chars = str.toCharArray();
		Stack<Character> stack = new Stack<>();
		for (char ca : chars) {
			switch (ca) {
				case '(':
				case '[':
				case '{':
					stack.push(ca);
					break;
				case ']':
				case ')':
				case '}':
					if (stack.isEmpty()) {
						return false;
					}
					Character pop = stack.pop();
					if (map.get(pop).equals(ca)) {
						continue;
					} else {
						return false;
					}
				default:
					return false;
			}
		}

		return stack.isEmpty();
	}

	public static void main(String[] args) {
		System.out.println(doVerify("(}"));
		System.out.println(doVerify("([)]{}"));
	}

}
